package cz.cvut.fit.web.nakupniSeznam.dao;

import javax.persistence.NoResultException;
import javax.persistence.Query;

import cz.cvut.fit.web.nakupniSeznam.constants.DBConstants;
import cz.cvut.fit.web.nakupniSeznam.data.List;

public class ListDAO extends GenericDAO<List> {
	
	public static final String REMOVED_FROM_USER= "removing complete";

	private static ListDAO instance = null;

	public static ListDAO getInstance() {
		if (instance == null) {
			instance = new ListDAO();
		}
		return instance;
	}

	@Override
	protected String getAllQueryName() {
		return "list.getAll";
	}

	@Override
	protected Class<List> getEntityClass() {
		return List.class;
	}
	
	@SuppressWarnings("unchecked")
	public java.util.List<List> getByOwner(String key) {
		Query q = this.em.createNamedQuery("list.getByOwner");
		q.setParameter("owner", key);

		try {
			return (java.util.List<List>) q.getResultList();
		} catch (NoResultException ex) {
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public java.util.List<List> getVisibleByOwner(String key) {
		Query q = this.em.createNamedQuery("list.getVisibleByOwner");
		q.setParameter("owner", key);

		try {
			return (java.util.List<List>) q.getResultList();
		} catch (NoResultException ex) {
			return null;
		}
	}
	
	public List getByOwnerAndId(String key, Long id) {
		Query q = this.em.createNamedQuery("list.getByOwnerAndId");
		q.setParameter("owner", key);
		q.setParameter("id", id);

		try {
			return (List) q.getSingleResult();
		} catch (NoResultException ex) {
			LOG.debug(DBConstants.NO_BD_RESULT);
			return null;
		}
	}
	
	public List getVisibleByOwnerAndId(String key, Long id) {
		Query q = this.em.createNamedQuery("list.getVisibleByOwnerAndId");
		q.setParameter("owner", key);
		q.setParameter("id", id);

		try {
			return (List) q.getSingleResult();
		} catch (NoResultException ex) {
			LOG.debug(DBConstants.NO_BD_RESULT);
			return null;
		}
	}
	
}
